﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;
using System.Diagnostics;

namespace SuperCalendar.Models
{
    public static class EventEntryHelper
    {

        public static EventEntryMeta Meta(this EventEntry eventEntry)
        {
            EventEntryMeta meta = new EventEntryMeta();
            if (String.IsNullOrEmpty(eventEntry.Content.Content))
            {
                return meta;
            }

            String content = eventEntry.Content.Content.Replace("&lt;", "<").Replace("&gt;", ">");
            int index = content.LastIndexOf("<meta>");
            if (index < 0)
            {
                return meta;
            }
            else
            {
                String metaStr = content.Substring(index);
                return EventEntryMeta.Parse(metaStr);
            }

        }

        public static void SetMeta(this EventEntry entry, EventEntryMeta meta)
        {
            if (meta == null)
                return;
            if (String.IsNullOrEmpty(entry.Content.Content))
            {
                entry.Content.Content = meta.ToString();
                return;
            }
            String content = entry.Content.Content.Replace("&lt;", "<").Replace("&gt;", ">");
            int index = content.LastIndexOf("<meta>");
            if (index < 0)
            {
                entry.Content.Content += meta.ToString();
            }
            else
            {
                content = content.Substring(0, index);
                entry.Content.Content = content + meta;
            }
        }

        public static String Title(this EventEntry entry)
        {
            return entry.Title.Text;
        }
        public static void SetTitle(this EventEntry entry, String title)
        {
            entry.Title.Text = title;
        }


        public static String Content(this EventEntry eventEntry)
        {
            String content = eventEntry.Content.Content;
            if (String.IsNullOrEmpty(content))
                return null;
            int index = content.LastIndexOf("&lt;meta&gt;");//<meta>
            if (index < 0)
                return content;
            return content.Substring(0, index);
        }

        public static void SetContent(this EventEntry eventEntry, String content)
        {
            if (String.IsNullOrEmpty(eventEntry.Content.Content))
                eventEntry.Content.Content = content;
            else
            {
                int index = eventEntry.Content.Content.LastIndexOf("&lt;meta&gt;");
                if (index < 0)
                {
                    eventEntry.Content.Content = content;
                    return;
                }
                String meta = eventEntry.Content.Content.Substring(index);
                eventEntry.Content.Content = content + meta;
            }
        }

        public static EventTime Time(this EventEntry entry)
        {
            When time = entry.Times[0];
            if (time != null)
            {
                return new EventTime(time.StartTime, time.EndTime);
            }
            return null;
        }

        public static void SetTime(this EventEntry entry, EventTime time)
        {
            if (time == null)
                return;
            When newTime = new When(time.StartTime, time.EndTime, time.AllDay);
            if (entry.Times.Count == 0)
            {
                entry.Times.Add(newTime);
            }
            else
            {
                entry.Times[0] = newTime;
            }
        }

        public static List<String> Locations(this EventEntry entry)
        {
            List<String> locations = new List<String>();
            foreach (Where w in entry.Locations)
            {
                locations.Add(w.ValueString);
            }

            return locations;
        }

        public static void SetLocations(this EventEntry entry, List<String> locations)
        {
            if (locations == null)
                return;
            entry.Locations.Clear();
            foreach (String l in locations)
            {
                entry.Locations.Add(new Where(null, l, null));
            }
        }

        public static String GoogleId(this EventEntry entry)
        {
            return entry.Id.Uri.ToString();
        }

        //public static String GoogleETag(this EventEntry entry)
        //{
        //    return entry.Etag;
        //}


        public static DateTime Published(this EventEntry entry)
        {
            return entry.Published;
        }

        public static void SetPublished(this EventEntry entry, DateTime time)
        {
            entry.Published = time;
        }

        public static DateTime Updated(this EventEntry entry)
        {
            return entry.Updated;
        }

        public static void SetUpdated(this EventEntry entry, DateTime time)
        {
            entry.Updated = time;
        }

        public static Event ToEvent(this EventEntry entry)
        {
            Event ev = new Event();
            ev.Title = entry.Title();
            ev.Content = entry.Content();
            ev.Category = entry.Meta().Category;
            ev.Priority = entry.Meta().Priority;
            ev.Completed = entry.Meta().Completed;
            ev.GoogleId = entry.GoogleId();
            ev.Time = entry.Time();
            ev.Locations = entry.Locations();
            ev.Published = entry.Published();
            ev.Updated = entry.Updated();
            return ev;
        }

        public static void FromEvent(this EventEntry entry, Event ev)
        {

            entry.SetTitle(ev.Title);
            entry.SetContent(ev.Content);
            EventEntryMeta meta = new EventEntryMeta(ev.Category, ev.Priority, ev.Completed);
            entry.SetMeta(meta);
            entry.SetTime(ev.Time);
            entry.SetLocations(ev.Locations);
            entry.SetPublished(ev.Published);
            entry.SetUpdated(ev.Updated);
        }
    }
}
